﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WorkRecord
{
    public partial class FormInput : Form
    {
        public FormInput(DateTime dateTime)
        {
            InitializeComponent();
            this.doc = new XmlDocument();
            this.doc.Load(@"./WorkRecord.xml");
            this.WorkRecord = doc.SelectSingleNode("WorkRecord");
            myDate.DateTime = dateTime;
            this.dateTimePicker.Value = dateTime;
        }

        private XmlDocument doc { get; set; }
        private XmlNode WorkRecord { get; set; }

        private MyDate myDate = new MyDate();

        XmlNode nodeYear { get; set; }
        XmlNode nodeMonth { get; set; }
        XmlNode nodeDay { get; set; }
        private void FormInput_Load(object sender, EventArgs e)
        {
            this.nodeYear = null;
            this.nodeMonth = null;
            this.nodeDay = null;

            foreach (XmlNode Year in WorkRecord.ChildNodes)
            {
                if (Convert.ToInt32(Year.Attributes["year"].Value) == myDate.ConverYear)
                {
                    this.nodeYear = Year;
                    foreach (XmlNode Month in Year.ChildNodes)
                    {
                        if (Convert.ToInt32(Month.Attributes["month"].Value) == myDate.ConverMonth)
                        {
                            this.nodeMonth = Month;
                            foreach (XmlNode Day in Month.ChildNodes)
                            {
                                if (Convert.ToInt32(Day.Attributes["day"].Value) == myDate.Day)
                                {
                                    this.nodeDay = Day;
                                    t_jb.Text = Day.Attributes["jbhour"].Value;
                                    t_qj.Text = Day.Attributes["qjhour"].Value;
                                    radioButton_gzr.Checked = Day.Attributes["type"].Value == "1";
                                    radioButton_xxr.Checked = Day.Attributes["type"].Value == "2";
                                    radioButton_jjr.Checked = Day.Attributes["type"].Value == "3";
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrWhiteSpace(t_jb.Text) || Convert.ToDecimal(t_jb.Text) <= 0) && (string.IsNullOrWhiteSpace(t_qj.Text) || Convert.ToDecimal(t_qj.Text) <= 0))
            {
                if (this.nodeYear != null && this.nodeMonth != null && this.nodeDay != null)
                {
                    this.nodeMonth.RemoveChild(this.nodeDay);
                    if (this.nodeMonth.ChildNodes.Count <= 0)
                    {
                        this.nodeYear.RemoveChild(this.nodeMonth);
                        if (this.nodeYear.ChildNodes.Count <= 0)
                        {
                            this.WorkRecord.RemoveChild(this.nodeYear);
                        }
                    }
                }
                this.doc.Save(@"./WorkRecord.xml");
                this.Close();
            }
            else
            {
                string type = radioButton_gzr.Checked ? "1" : (radioButton_xxr.Checked ? "2" : radioButton_jjr.Checked ? "3" : "0");
                if (this.nodeDay == null)
                {
                    XmlElement dayElement = doc.CreateElement("Day");
                    dayElement.SetAttribute("day", myDate.Day.ToString());
                    dayElement.SetAttribute("jbhour", string.IsNullOrWhiteSpace(t_jb.Text) ? "0" : t_jb.Text);
                    dayElement.SetAttribute("qjhour", string.IsNullOrWhiteSpace(t_qj.Text) ? "0" : t_qj.Text);

                    dayElement.SetAttribute("type", type); // type 1:工作日    2:周末    3:节假日
                    if (this.nodeMonth == null)
                    {
                        XmlElement monthElement = doc.CreateElement("Month");
                        monthElement.SetAttribute("month", myDate.ConverMonth.ToString());
                        monthElement.AppendChild(dayElement);
                        if (this.nodeYear == null)
                        {
                            XmlElement yearElement = doc.CreateElement("Year");
                            yearElement.SetAttribute("year", myDate.ConverYear.ToString());
                            yearElement.AppendChild(monthElement);
                            this.WorkRecord.AppendChild(yearElement);
                        }
                        else
                        {
                            this.nodeYear.AppendChild(monthElement);
                        }
                    }
                    else
                    {
                        this.nodeMonth.AppendChild(dayElement);
                    }
                }
                else
                {
                    this.nodeDay.Attributes["jbhour"].Value = t_jb.Text;
                    this.nodeDay.Attributes["qjhour"].Value = t_qj.Text;
                    this.nodeDay.Attributes["type"].Value = type; // type 1:工作日    2:周末    3:节假日
                }
                this.doc.Save(@"./WorkRecord.xml");
                this.Close();
            }
        }

        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                e.KeyChar = '.';
            }
            int kc = e.KeyChar;
            string text = (sender as TextBox).Text;
            if ((kc < 48 || kc > 57) && kc != 8 && kc != 46)
                e.Handled = true;
            if (kc == 46)                       
            {
                if (text.Length <= 0)
                    e.Handled = true;          
                else
                {
                    decimal f;
                    decimal oldf;
                    bool b1 = false, b2 = false;
                    b1 = decimal.TryParse(text, out oldf);
                    b2 = decimal.TryParse(text + e.KeyChar.ToString(), out f);
                    if (b2 == false)
                    {
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

    }
}